package baiqitun.stupro.project.service.impl;

import baiqitun.stupro.common.exception.enums.support.NotFoundExceptionEnum;
import baiqitun.stupro.common.exception.support.ExistedException;
import baiqitun.stupro.common.exception.support.NotFoundException;
import baiqitun.stupro.common.exception.support.RelationExistedException;
import baiqitun.stupro.common.service.support.MongoServiceImpl;
import baiqitun.stupro.project.entity.SysProject;
import baiqitun.stupro.project.entity.SysProjectLike;
import baiqitun.stupro.project.service.SysProjectLikeService;
import baiqitun.stupro.project.service.SysProjectService;
import baiqitun.stupro.security.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author kedaji
 */
@Service
public class SysProjectLikeServiceImpl extends MongoServiceImpl<SysProjectLike> implements SysProjectLikeService {
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysProjectService sysProjectService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void like(String userId, String projectId, String createBy) {
        sysProjectService.checkNotFound(projectId, new NotFoundException(NotFoundExceptionEnum.PROJECT_NOT_FOUND));
        sysUserService.checkEnabled(userId);
        SysProjectLike queryProjectLike = new SysProjectLike();
        queryProjectLike.setUserId(userId).setProjectId(projectId);
        boolean exists = repository.exists(Example.of(queryProjectLike));
        if (exists){
            //用户已经给项目点赞过了
            throw new RelationExistedException();
        }
        queryProjectLike.setCreateBy(createBy);
        save(queryProjectLike);
    }
}
